package com.veer.shop.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串校验、转化工具类
 * 
 * @author WangBin
 * 2020年8月17日-下午5:41:17
 */
public class StringUtil {
	
	public final static String EMAIL_REGEX = "^\\s*?(.+)@(.+?)\\s*$";
	public final static String PHONE_REGEX = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
	private static Pattern linePattern = Pattern.compile("_(\\w)");
	
	/**
	 * 校验邮箱
	 * 校验通过返回 true，反之 false
	 * 
	 * @param email
	 * @return boolean
	 */
	public static boolean validEmail(String email) {
		Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
		Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
        return emailMatcher.matches();
    }
	
	/**
	 * 校验手机号
	 * 校验通过返回 true，反之 false
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean validPhone(String phone) {
		Pattern PHONE_PATTERN = Pattern.compile(PHONE_REGEX);
		Matcher phoneMatcher = PHONE_PATTERN.matcher(phone);
        return phoneMatcher.matches();
    }
	
	public static String toSnake(String string) {
		string = string.toLowerCase();
        Matcher matcher = linePattern.matcher(string);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
	}
}
